﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestSphere : MonoBehaviour
{
    //时间间隔
    public float timeInterval = 0.1f;
    //当前时间
    private float curTime;
    //tail的数量
    public int tailSum = 0;
    //tail距离
    private float tailVar = 0.1f;



    public Transform m_Tail_Tra;   // 这个一定不要放在跳动的物体下面
    [HideInInspector]

    public List<Vector3> posList = new List<Vector3>();

    void Start()
    {
        Init();
    }

    /// <summary>
    /// 初始化
    /// </summary>
    public void Init()
    {
        // 先隐藏所有拖尾
        for(int i = 0; i < m_Tail_Tra.childCount; i++)
        {
            m_Tail_Tra.GetChild(i).gameObject.SetActive(false);
        }
    }



    void Update()
    {
        // 测试
        //if (Input.GetKeyDown(KeyCode.A))
        //{
        //    OnAddTail();
        //}

        OnUpdate_TailPos();
    }

    public void OnUpdate_TailPos()
    {
        if (tailSum <= 0) return;
        if (curTime >= timeInterval)
        {
            curTime = 0;
            OnRefreshTailPos();
        }
        else
        {
            curTime += Time.deltaTime;
        }
    }

    public void OnAddTail(int num = 1)
    {
        tailSum += num;

        if (tailSum > posList.Count)
        {
            int n = tailSum - posList.Count;
            for(int i = 0; i < n; i++)
            {
                posList.Add(this.transform.position);
            }
        }

        GameObject obj;
        for (int i = 0; i < tailSum; i++)
        {
            if (i < m_Tail_Tra.childCount)
            {
                obj = m_Tail_Tra.GetChild(i).gameObject;
                obj.SetActive(true);
                obj.GetComponent<TailMove>().SetEndPos(posList[i]);
            }
            else
            {
                obj = Instantiate(m_Tail_Tra.GetChild(0).gameObject, m_Tail_Tra);

                // 这里是给新的拖尾一个大小
                float _var = 1 - tailVar * tailSum;
                if (_var < 0.1f)
                {
                    _var = 0.5f;
                }

                obj.transform.localScale = obj.transform.localScale * _var;
                obj.transform.position = new Vector3(obj.transform.position.x, obj.transform.position.y, obj.transform.position.z + 1);
                obj.SetActive(true);
                obj.GetComponent<TailMove>().Init(posList[i]);
            }
        }
    }

    public void OnRefreshTailPos()
    {
        if (posList.Count <= 0) return;
        Vector3 pos;

        // 在第一个刷新成最新坐标，后面左边替换成上一个坐标
        if (posList.Count > 1)
        {
            for (int i = 0; i < posList.Count - 1; i++)
            {
                pos = posList[posList.Count - i - 2];

                posList[posList.Count - i - 1] = posList[posList.Count - i - 2];

                posList[posList.Count - i - 2] = pos;
            }
        }

        posList[0] = this.transform.position;

        for(int i = 0; i < m_Tail_Tra.childCount; i++)
        {
            m_Tail_Tra.GetChild(i).GetComponent<TailMove>().SetEndPos(posList[i]);
        }
    }

}